//
//  Extension+collection.swift
//  ShiGuangJiaSwift
//
//  Created by 汪志浩 on 2023/4/25.
//

import Foundation

public extension UICollectionView {
    // MARK: - Cell register and reuse
    /**
     Register cell nib
     
     - parameter aClass: class
     */
    func fy_registerCellNib<T: UICollectionViewCell>(_ aClass: T.Type) {
        let name = String(describing: aClass)
        let nib = UINib(nibName: name, bundle: nil)
        self.register(nib, forCellWithReuseIdentifier: name)
    }
    
    /**
     Register cell class
     
     - parameter aClass: class
     */
    func fy_registerCellClass<T: UICollectionViewCell>(_ aClass: T.Type) {
        let name = String(describing: aClass)
        self.register(aClass, forCellWithReuseIdentifier: name)
    }
    
    /**
     Reusable Cell
     
     - parameter aClass:    class
     
     - returns: cell
     */
    func fy_dequeueReusableCell<T: UICollectionViewCell>(_ aClass: T.Type ,for indexPath:IndexPath) -> T {
        let name = String(describing: aClass)
        guard let cell = dequeueReusableCell(withReuseIdentifier: name, for: indexPath) as? T else {
            fatalError("\(name) is not registed")
        }
        return cell
    }
    
    // MARK: - HeaderFooter register and reuse
    /**
     Register cell nib
     
     - parameter aClass: class
     */
    func fy_registerHeaderFooterNib<T: UICollectionReusableView>(_ aClass: T.Type,forSupplementaryViewOfKind kind: String) {
        let name = String(describing: aClass)
        let nib = UINib(nibName: name, bundle: nil)
//        self.register(nib, forHeaderFooterViewReuseIdentifier: name)
        self.register(nib, forSupplementaryViewOfKind: kind, withReuseIdentifier: name)
    }
    
    /**
     Register cell class
     
     - parameter aClass: class
     */
    func fy_registerHeaderFooterClass<T: UICollectionReusableView>(_ aClass: T.Type,forSupplementaryViewOfKind kind: String) {
        let name = String(describing: aClass)
        self.register(aClass, forSupplementaryViewOfKind: kind, withReuseIdentifier: name)
    }
    
    /**
     Reusable Cell
     
     - parameter aClass:    class
     
     - returns: cell
     */
    func fy_dequeueReusableHeaderFooter<T: UICollectionReusableView>(_ aClass: T.Type,forSupplementaryViewOfKind kind: String,for indexPath:IndexPath) -> T {
        let name = String(describing: aClass)
        
        guard let cell = dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: name, for: indexPath) as? T else {
            fatalError("\(name) is not registed")
        }
        return cell
    }
}
